package com.tianbing.adlottery.controller.supplieradmin;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.jubao.ejb.dao.ProductDao;
import com.jubao.ejb.entity.Product;
import com.tianbing.adlottery.util.ControllerFormBean;

public class EditProductProfileController extends SimpleFormController {
	static Logger logger = Logger.getLogger(EditProductProfileController.class.getName());


	private ProductDao productDao;

	// ------------------GET Request LifeCycle------------------
	protected Object formBackingObject(HttpServletRequest request) throws Exception {
		ControllerFormBean fb = new ControllerFormBean();
		long prdsid = ServletRequestUtils.getLongParameter(request, "prdsid", -1);
		Product product = productDao.find(prdsid);
		fb.setProduct(product);
		return fb;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse, java.lang.Object,
	 *      org.springframework.validation.BindException)
	 */
	protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object cmd,
			BindException ex) throws Exception {

		ControllerFormBean form = (ControllerFormBean) cmd;

		Product product = form.getProduct();
		product = productDao.save(product);
		ModelAndView mv = new ModelAndView(this.getSuccessView()+"?prdsid="+product.getSysId());
		return mv;
	}

	protected Map referenceData(HttpServletRequest request) throws Exception {
		Map model = new HashMap();
		return model;
	}

	public static void setLogger(Logger logger) {
		EditProductProfileController.logger = logger;
	}

	public void setProductDao(ProductDao productDao) {
		this.productDao = productDao;
	}

}
